package com.wisdomparking.task;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.wisdomparking.bean.Rawdata;
import com.wisdomparking.service.ParkingService;
import com.wisdomparking.service.RawdataService;
import com.wisdomparking.tools.SpringContextUtils;
import com.wisdomparking.tools.ToolDataHandle;
import com.wisdomparking.tools.ToolValid;

public class RawDataMonitor {
	
	private static ParkingService parkingService = (ParkingService)SpringContextUtils.getBeanByClass(ParkingService.class);
	private static RawdataService rawdataService = (RawdataService)SpringContextUtils.getBeanByClass(RawdataService.class);
	
	public void dataListen() {
		Map<String, Object> map = new HashMap<>();
		map.put("putInStorageMark", 0);
		List<Rawdata> rawDataList = rawdataService.getRawdataList(map);
		
		if(ToolValid.isNotEmpty(rawDataList)){
			for(Rawdata rawData: rawDataList){
				ToolDataHandle.dataHandle(rawdataService, parkingService, rawData);
			}
		}
	}
	
	public void addRawdataDB(String identification, String realTimeData) {
		Rawdata rawdata = rawdataService.getRawdataByIdentification(identification);
		if(ToolValid.isNull(rawdata)) {
			rawdata = new Rawdata();
			rawdata.setIdentification(identification);
			rawdata.setRealTimeData(realTimeData);
			rawdata.setPutInStorageMark(false);
			rawdata.setUpdateTime(new Date());
			rawdataService.addRawdata(rawdata, true);
		}
		else {
			rawdata.setIdentification(identification);
			rawdata.setRealTimeData(realTimeData);
			rawdata.setPutInStorageMark(false);
			rawdata.setUpdateTime(new Date());
			rawdataService.updateRawdata(rawdata, true);
		}
		
	}
	
}
